package net.pimpas.web.jsf.renderers.input.ajax;

import java.io.IOException;

import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import net.pimpas.web.jsf.renderers.AjaxRendererBase;
import net.pimpas.web.jsf.renderers.RendererBase;
import net.pimpas.web.jsf.util.ComponentUtils;

public class AjaxInputRenderer extends RendererBase implements AjaxRendererBase {
	
	public static final String JAVASCRIPT_HANDLER	= "pimpas.AJAX.Populate.Input";
	
	public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
		if(!component.isRendered()) return;
		if(!ComponentUtils.isCorrectComponentType(component, UIInput.class)) return;
		
		// get input value
        String value = org.apache.myfaces.shared_impl.renderkit.RendererUtils.getStringValue(context, component);
        ResponseWriter writer = context.getResponseWriter();
        writer.startElement("value", component);
        writer.write(value);
        writer.endElement("value");
	}
	
	public String getJavascriptHandler() {
		return JAVASCRIPT_HANDLER;
	}

	public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
	}

	public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
	}
}